package com.leyou.common.advice;

import com.leyou.common.exceptions.LyException;
import com.leyou.common.vo.ExceptionResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * 统一异常处理类
 * -@ControllerAdvice能够拦截所有controller抛出的异常,拦截器
 *
 */
@ControllerAdvice
public class BasicExceptionAdvice {

    /**
     * 有异常的时候
     * 返回错误状态码和错误信息
     */
    @ExceptionHandler(value = RuntimeException.class)
    public ResponseEntity<ExceptionResult> runTimeExceptionHandler(RuntimeException e){

//        错误信息
        String message = e.getMessage();
        return ResponseEntity.status(500).body(new ExceptionResult(500,e.getMessage()));
    }

    @ExceptionHandler(value = LyException.class)
    public ResponseEntity<ExceptionResult> lyExceptionHandler(LyException e){
        return ResponseEntity.status(e.getStatus()).body(new ExceptionResult(e.getStatus(),e.getMessage()));
    }


}
